﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vit.Web.DataAnnotations
{
    [AttributeUsage(AttributeTargets.Property)]
    public class ReferenceAttribute : Attribute
    {
        public ReferenceAttribute(Type referenceType)
        {
            ReferenceType = referenceType;
            ReferenceMethod = ReferenceMethod.DropdownList;
            Init();
        }
        
        public ReferenceAttribute(Type referenceType, ReferenceMethod method)
        {
            ReferenceType = referenceType;
            ReferenceMethod = method;
            Init();
        }

        public ReferenceAttribute(Type referenceType, ReferenceMethod method, string controller, string buttonText, string nullDisplayText)
        {
            ReferenceType = referenceType;
            ReferenceMethod = method;
            Controller = controller;
            ButtonText = buttonText;
            NullDisplayText = nullDisplayText;
            Init();
        }

        private void Init()
        {
            if (string.IsNullOrEmpty(ButtonText))
                ButtonText = "...";
            if (string.IsNullOrEmpty(Controller))
                Controller = ReferenceType.Name;
            if (string.IsNullOrEmpty(NullDisplayText))
                NullDisplayText = " ";
        }

        public Type ReferenceType { get; set; }
        public ReferenceMethod ReferenceMethod { get; set; }
        public string NullDisplayText { get; set; }
        public string ButtonText { get; set; }
        public string Controller { get; set; }
        public string DataSourceName { get; set; }
        public string DialogTitle { get; set; }
        public object Arguments { get; set; }
    }

    public enum ReferenceMethod
    {
        DropdownList, PopupDialog
    }
}
